package com.icity.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.icity.entity.Menu;
import com.icity.entity.Role;
import com.icity.entity.RoleUser;
import com.icity.entity.User;
import com.icity.entity.system.MenuListSupport;
import com.icity.entity.system.RoleMenu;
import com.icity.entity.system.TableSupport;
import com.icity.mapper.SystemMapper;
import com.icity.service.SystemService;

@Service
public class SystemServiceImpl implements SystemService {

	@Autowired
	private SystemMapper systemMapper;

	@Override
	public List<MenuListSupport> getMenuListByUser(Map<Object, Object> map) {
		List<Menu> list = systemMapper.getMenuListByRolesCode(map);
		List<MenuListSupport> listMenu = new ArrayList<MenuListSupport>();
		for (Menu menu : list) {
			MenuListSupport menuListSupport = new MenuListSupport();
			// 判断是否为一级菜单
			if (menu.getParent_code().equals("0")) {
				menuListSupport.setName(menu.getName());
				menuListSupport.setSubShow(false);
				menuListSupport.setPoster(menu.getIcon_code());
				List<Map<String, String>> data = new ArrayList<Map<String, String>>();
				for (Menu menuSub : list) {
					if (menuSub.getParent_code().equals(menu.getUnique_code())) {
						Map<String, String> mapSub = new HashMap<String, String>();
						mapSub.put("name", menuSub.getName());
						mapSub.put("url", menuSub.getUrl());
						mapSub.put("id", menuSub.getUnique_code());
						data.add(mapSub);
					}
				}
				menuListSupport.setSubItems(data);
				listMenu.add(menuListSupport);
			}
		}
		return listMenu;
	}

	@Override
	public TableSupport<Menu> getMenuLsit(Map<Object, Object> map) {
		TableSupport<Menu> listTable = new TableSupport<Menu>();
		List<Menu> listMenu = systemMapper.getMenuList(map);
		int count = systemMapper.getMenuCount(map);
		for (Menu menu : listMenu) {
			if (menu.getParent_code().equals("0")) {
				menu.setParent_name("菜单");
			} else {
				menu.setParent_name(systemMapper.getMenuParentNameBycode(menu.getParent_code()));
			}
		}

		listTable.setCode(0);
		listTable.setMsg(null);
		listTable.setCount(count);
		listTable.setData(listMenu);
		return listTable;
	}

	@Override
	public int deleteMenuById(int id) {
		return systemMapper.deleteMenuById(id);
	}

	@Override
	public int deleteUserById(int id) {
		return systemMapper.deleteUserById(id);
	}

	@Override
	public List<Map<Object,Object>> getParentMen() {
		List<Menu> list = systemMapper.getParentMenu();
		List<Map<Object,Object>> listParent = new ArrayList<Map<Object,Object>>();
		Map<Object,Object> map = new HashMap<Object,Object>();
		map.put("key", "主菜单");
		map.put("value", 0);
		listParent.add(map);
		for(Menu menu : list){
			Map<Object,Object> map1 = new HashMap<Object,Object>();
			map1.put("key", menu.getName());
			map1.put("value", menu.getUnique_code());
			listParent.add(map1);
		}
		return listParent;
	}

	@Override
	public int saveMenu(Menu menu) {
		return systemMapper.insertMenu(menu);
	}

	@Override
	public int updateMenu(Menu menu) {
		return systemMapper.updateMenu(menu);
	}

	@Override
	public boolean isExitMenuByName(int id) {
		int count = systemMapper.getMenuCountByName(id);
		boolean flag = false;
		if(count>0){
			flag = true;
		}
		return flag;
	}

	@Override
	public int saveRoleMenu(RoleMenu roleMenu) {
		return systemMapper.insertRoleMenu(roleMenu);
	}

	@Override
	public List<User> getUserInfo(Map<Object, Object> map) {
		return systemMapper.getUserInfo(map);
	}

	@Override
	public TableSupport<User> getUserList(Map<Object, Object> map) {
		TableSupport<User> listTable = new TableSupport<User>();
		List<User> listUser = systemMapper.getUserList(map);
		int count = systemMapper.getUserCount(map);
		for (User user : listUser) {
			String role_name = systemMapper.getRoleNameByUserId(user.getUnique_code());
			user.setRole_name(role_name);
		}

		listTable.setCode(0);
		listTable.setMsg(null);
		listTable.setCount(count);
		listTable.setData(listUser);
		return listTable;
	}

	@Override
	public int saveUser(User user) {
		return systemMapper.insertUser(user);
	}

	@Override
	public int saveRoleUser(RoleUser roleUser) {
		return systemMapper.insertRoleUser(roleUser);
	}

	@Override
	public int updateUser(User user) {
		System.out.println("name:"+user.getName());
		return systemMapper.updateUser(user);
	}

	@Override
	public int updateRoleUser(RoleUser user) {
		return systemMapper.updateRoleUser(user);
	}

	@Override
	public boolean isExitUserByName(int id) {
		int count = systemMapper.getUserCountByName(id);
		boolean flag = false;
		if(count>0){
			flag = true;
		}
		return flag;
	}

	@Override
	public List<Map<Object,Object>> getParentUser() {
		List<Role> list = systemMapper.getParentUser();
		List<Map<Object,Object>> listParent = new ArrayList<Map<Object,Object>>();
		for(Role role : list){
			Map<Object,Object> map = new HashMap<Object,Object>();
			map.put("key", role.getName());
			map.put("value", role.getUnique_code());
			listParent.add(map);
		}
		return listParent;
	}

	@Override
	public int updatePwd(String phone, String pwd) {
		return systemMapper.updatePwd(phone, pwd);
	}

}
